import React, {useEffect, useState} from "react";
import {
    Button,
    Col,
    Form,
    Input,
    message,
    Modal,
    Row,
    Select,
    Space,
    Switch,
    Table,
    TableProps,
    Tooltip,
} from 'antd';
import renderIcons from "@/components/CustomIconSelector/conponents";
import {DeleteOutlined} from "@ant-design/icons";
import {useNavigate} from "react-router-dom";
import {definitionStatus, start, taskList} from "@/api/flowable/myProcess/index.ts";
import {complete, todoList} from "@/api/flowable/taskTodo/index.ts";
import useResponsiveLayout from "@/utils/useResponsiveLayout";
import { globalMessage } from "@/utils/message";

const taskTodoView: React.FC = () => {
    const initialQueryParams = {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
    };

    interface DataType {
    }

    const navigateTo = useNavigate();
    const [form] = Form.useForm();
    const [forceUpdateKey, setForceUpdateKey] = useState(0); // 用于强制更新组件的 key
    const [dataList, setDataList] = useState<DataType[]>([]);
    const columns: TableProps<DataType>['columns'] = [
        {
            title: '任务编号',
            dataIndex: 'procInsId',
            key: 'procInsId',
            align: 'center'
        },
        {
            title: '流程名称',
            dataIndex: 'procDefName',
            key: 'procDefName',
            align: 'center'
        },
        {
            title: '任务节点',
            dataIndex: 'taskName',
            key: 'taskName',
            align: 'center'
        }, {
            title: '流程类别',
            dataIndex: 'category',
            key: 'category',
            align: 'center'
        }, {
            title: '版本',
            dataIndex: 'procDefVersion',
            key: 'procDefVersion',
            align: 'center'
        }, {
            title: '业务主键',
            dataIndex: 'businessKey',
            key: 'businessKey',
            align: 'center'
        }, {
            title: '流程发起人',
            dataIndex: 'startUserName',
            key: 'startUserName',
            align: 'center',
            render: (_, record:any) => (
                <div>{record.startUserName}{record.startDeptName}</div>
            )
        }, {
            title: '接收时间',
            dataIndex: 'createTime',
            key: 'createTime',
            align: 'center'
        }, {
            title: '操作',
            dataIndex: 'action',
            key: 'action',
            render: (_, record:any) => (
                <Space size="middle">
                        <Tooltip title="审批">
                            <Button
                                type="link"
                                size="small"
                                icon={renderIcons("edit")}
                                onClick={() => CompleteClick(record)}/>
                        </Tooltip>
                </Space>
            ),
        },
    ];
    const [queryParams, setQueryParams] = useState(initialQueryParams);
    const [total, setTotal] = useState(0);
    const [openDataPermissions, setOpenDataPermissions] = useState<boolean>(false);
    useEffect(() => {
        getList();
    }, [queryParams]);  // 监听 queryParams 变化，调用 getList

    /** 查询用户列表 */
    function getList() {
        todoList(queryParams).then((res:any) => {
            setDataList(res.rows.map((item: any) => ({
                ...item,
                // key: item.id // 使用 userId 作为唯一的 key
            })))
            setTotal(res.total); // 更新 total，用于分页
        });
    }

    // 查询列表
    function handleSearch() {
        form.validateFields().then(values => {
            setQueryParams(prevQueryParams => ({
                ...queryParams,
                name: values.name,
            }));
        });
    }

    // 重置查询
    const resetSearch = () => {
        form.resetFields();
        setQueryParams(initialQueryParams); // 重置为初始状态
    }

    // 分页变化
    const handleTableChange = (page: number, pageSize: number) => {
        setQueryParams({
            ...queryParams,
            pageNum: page,
            pageSize: pageSize,
        });
    };
    // 审批按钮
    const CompleteClick = (e: any) => {
        const data = {
            deployId: e.deployId,
            taskId: e.taskId,
            procInsId: e.procInsId,
            comment: "正常意见"+ new Date().getTime(),
            variables: {
                name: "张三" + new Date().getTime(),
            }
        }
        complete(data).then((res:any) => {

            if (res.code == 200) {
                 globalMessage().success(res.msg);
                 getList()
            }
        });
    }

    return (
        <div className='home'>
            <Row gutter={20} style={{paddingTop: "20px"}}>
                <Col span={24}>
                    <div><Form
                        form={form}
                        name="definition_form_controls"
                        style={{marginTop: 10, marginLeft: 10, marginBottom: 10}}
                       layout={useResponsiveLayout("inline")}>
                        <Form.Item
                            name="name"
                            label="流程名称">
                            <Input/>
                        </Form.Item>
                        <Form.Item>
                            <Button type="primary" htmlType="submit" onClick={handleSearch} style={{
                                marginRight: 20,
                            }}>
                                查询
                            </Button>
                            <Button type="primary" htmlType="reset" onClick={resetSearch}>
                                重置
                            </Button>
                        </Form.Item>
                    </Form></div>
                    <Table<DataType> rowKey="taskId"
                                     columns={columns}
                                     dataSource={dataList}
                                     scroll={{ x: true }}
                                     pagination={{
                                         current: queryParams.pageNum,
                                         pageSize: queryParams.pageSize,
                                         total: total,
                                         showSizeChanger: true, // 显示分页尺寸切换器
                                         pageSizeOptions: ['10', '20', '50'], // 分页尺寸选项
                                         onShowSizeChange: handleTableChange, // 分页尺寸变化时的回调
                                         onChange: handleTableChange
                                     }}/>
                </Col>
            </Row>
        </div>
    )
};
export default taskTodoView;
